<?php
class User
{
    /**
     * @var object Zend DB Adapter
     */
    private $_db;
    /**
     * @var string
     */
   
    public function __construct()
    {
		$this->_db = Zend_Registry::get('db');
    }
    public function addUser($insertarr)
    {

         try
         {
             $sql= " insert  into aml_admin (
                    admin_firstname ,
                    admin_lastname ,
                    admin_email ,
                    admin_username ,
                    admin_password ,
                    admin_userType ,
                    admin_status 
                      ) values
                    (
                    
                    '{$insertarr['admin_firstname']}',
                    '{$insertarr['admin_lastname']}',
                    '{$insertarr['admin_email']}',
                    '{$insertarr['admin_username']}',
                    '".md5($insertarr['admin_password'])."',
                    '{$insertarr['admin_userType']}',
                    '{$insertarr['admin_status']}'
                    
                    )";
            
          //echo $sql;exit;
              if($this->_db->exec($sql))
              {
                  return true;
              }
              else
              {
                  return false;
              }
         }
         catch(Exception $e)
         {
              echo 'Caught exception: ',  $e->getMessage(), "\n";
             return false;
         }
    }

     public function editUser($insertarr)
    {
         if(!empty($insertarr['admin_password']))
         {
             $pwdtext=" , admin_password='".md5($insertarr['admin_password'])."' ";
         }
         else{
             $pwdtext="   ";
         }

         try
         {
             $sql= " update  aml_admin
                   set  admin_firstname='{$insertarr['admin_firstname']}' ,
                    admin_lastname='{$insertarr['admin_lastname']}' ,
                    admin_email='{$insertarr['admin_email']}' ,
                    admin_username='{$insertarr['admin_username']}' 
                   ".$pwdtext .",
                    admin_userType='{$insertarr['admin_userType']}' ,
                    admin_status='{$insertarr['admin_status']}' where admin_id={$insertarr['admin_id']}
               
                    ";
                 //   echo $sql;exit;
              if($this->_db->exec($sql))
              {
                  return true;
              }
              else
              {
                  return false;
              }
         }
         catch(Exception $e)
         {
              echo 'Caught exception: ',  $e->getMessage(), "\n";
             return false;
         }
    }
    public function listUsers()
    {
        $sql=" select * from aml_admin";
        return $this->_db->fetchAll($sql);

    }

      public function listUsersforhome()
    {
          $arr=array();
        $sql=" select * from aml_Usercontent";
        $Userlist= $this->_db->fetchAll($sql);
        foreach($Userlist as $key=>$val)
        {

            $arr[$key]=$val;
            
        }
       return  $arr;
    }


      public function getUser($admin_id)
    {
        $sql=" select * from aml_admin where admin_id={$admin_id}";
        return $this->_db->fetchRow($sql);

    }

    
    public  function deleteUser($admin_id)
    {
         $sql=" delete from aml_admin where admin_id={$admin_id}";
       //echo $sql;exit;
           if($this->_db->exec($sql))
              {
                  return true;
              }
              else
              {
                  return false;
              }
    }
    public function changeUserStatus($admin_id)
    {
         $sql_getstatus = "SELECT admin_status
				FROM aml_admin
				WHERE  admin_id = " . $admin_id;

		$result = $this->_db->fetchRow($sql_getstatus);
		$status = $result['admin_status'];

		if($status == 1) {
			$change_status = 0;
		} else {
			$change_status = 1;
		}

		$sql_updatestatus = "UPDATE aml_admin
							 SET admin_status = " . $change_status . "
							 WHERE  admin_id = " . $admin_id;
               // echo $sql_updatestatus;
		$this->_db->exec($sql_updatestatus);	
    }
	public function getcontent($id)
        {
            $sql=" select full_text from aml_Usercontent where id={$id}";
            $content= $this->_db->fetchRow($sql);
            return $content["full_text"];
        }
}